<template>
  <div class="app-container" v-loading="stlxLoading">
    <h2 style="text-align: center">字体和七步法</h2>

    <el-steps
      :active="4"
      process-status="wait"
      align-center
      style="margin-bottom: 40px"
    >
      <el-step title="添加字体" />
      <el-step title="添加七步" />
    </el-steps>
    <div>
      <el-form :inline="true">
        <el-form-item>
          <el-select
            v-model="buzhou"
            placeholder="请选择类型"
            @change="buzhouChange"
          >
            <el-option label="欣赏" :value="0"> </el-option>
            <el-option label="认读" :value="1"> </el-option>
            <el-option label="观察" :value="2"> </el-option>
            <el-option label="分析" :value="4"> </el-option>
            <el-option label="笔法" :value="3"> </el-option>
            <el-option label="视频" :value="7"> </el-option>
            <el-option label="对比" :value="5"> </el-option>
            <el-option label="随堂练习" :value="6"> </el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </div>
    <el-form
      :rules="dataRule"
      ref="practiceForm"
      :model="practice"
      label-width="120px"
    >
      <el-form-item label="观察书写图片" prop="gcsxPicture">
        <el-upload
          class="avatar-uploader"
          :action="'http://121.41.167.195:8000/api/oss/upload'"
          :show-file-list="false"
          :on-success="handleAvatarSuccess1"
        >
          <img
            v-if="practice.gcsxPicture"
            :src="practice.gcsxPicture"
            class="avatar"
          />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item label="对比描红图片" prop="dbmhPicture">
        <el-upload
          class="avatar-uploader"
          :action="'http://121.41.167.195:8000/api/oss/upload'"
          :show-file-list="false"
          :on-success="handleAvatarSuccess2"
        >
          <img
            v-if="practice.dbmhPicture"
            :src="practice.dbmhPicture"
            class="avatar"
          />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item label="调整描红图片" prop="tzmhPicture">
        <el-upload
          class="avatar-uploader"
          :action="'http://121.41.167.195:8000/api/oss/upload'"
          :show-file-list="false"
          :on-success="handleAvatarSuccess3"
        >
          <img
            v-if="practice.tzmhPicture"
            :src="practice.tzmhPicture"
            class="avatar"
          />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item label="观察书写图片" prop="sgtxPicture">
        <el-upload
          class="avatar-uploader"
          :action="'http://121.41.167.195:8000/api/oss/upload'"
          :show-file-list="false"
          :on-success="handleAvatarSuccess4"
        >
          <img
            v-if="practice.sgtxPicture"
            :src="practice.sgtxPicture"
            class="avatar"
          />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item label="临摹图片" prop="lmPicture">
        <el-upload
          class="avatar-uploader"
          :action="'http://121.41.167.195:8000/api/oss/upload'"
          :show-file-list="false"
          :on-success="handleAvatarSuccess5"
        >
          <img
            v-if="practice.lmPicture"
            :src="practice.lmPicture"
            class="avatar"
          />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
    </el-form>

    <div>
      <el-button @click="previous">上一步</el-button>
      <el-button :disabled="saveBtnDisabled" type="primary" @click="next"
        >保存</el-button
      >
    </div>
  </div>
</template>

<script>
import Vue from "vue";
export default {
  data() {
    return {
      buzhou: 6,
      stlxLoading: false,
      saveBtnDisabled: false, // 保存按钮是否禁用
      practice: {
        // 出版社对象
        sgtxPicture: "",
        tzmhPicture: "",
        dbmhPicture: "",
        gcsxPicture: "",
        lmPicture: "",
        tid: null,
        id: null,
      },
      xzBtn: false,
      type: 0, // 0:添加1:修改
      dialogObserveVisible: false,
      saveObserveBtnDisabled: false,
      dialogObservePictureVisible: false,
      saveRdPictureBtnDisabled: false,
      dataRule: {
        gcsxPicture: [{ required: true, message: "图片不能为空" }],
        dbmhPicture: [{ required: true, message: "图片不能为空" }],
        tzmhPicture: [{ required: true, message: "图片不能为空" }],
        sgtxPicture: [{ required: true, message: "图片不能为空" }],
      },
    };
  },
  
  activated() {
    this.buzhou = 6;
    this.practice.tid = this.$route.params.id;
    // 有数据就要回显数据
    this.getPracticeByWordId();
  },
  methods: {
    buzhouChange(value) {
      switch (value) {
        case 0:
          // 欣赏
          this.$router.push({
            path: "/ruanbi/ruanbixsAdd/" + this.$route.params.id,
          });
          break;
        case 1:
          // 认读
          this.$router.push({
            path: "/ruanbi/ruanbirdAdd/" + this.$route.params.id,
          });
          break;
        case 2:
          // 观察
          this.$router.push({
            path: "/ruanbi/ruanbigcAdd/" + this.$route.params.id,
          });
          break;
        case 3:
          // 笔法
          this.$router.push({
            path: "/ruanbi/ruanbibfAdd/" + this.$route.params.id,
          });
          break;
        case 4:
          // 分析
          this.$router.push({
            path: "/ruanbi/ruanbifxAdd/" + this.$route.params.id,
          });
          break;
        case 5:
          // 对比
          this.$router.push({
            path: "/ruanbi/ruanbidbAdd/" + this.$route.params.id,
          });
          break;
        case 6:
          // 随堂练习
          this.$router.push({
            path: "/ruanbi/ruanbistlxAdd/" + this.$route.params.id,
          });
          break;
        default:
          // 视频
          this.$router.push({
            path: "/ruanbi/wordVideo/" + this.$route.params.id,
          });
          break;
      }
    },
    //上传成功后的回调函数
    handleAvatarSuccess1(response) {
      this.practice.gcsxPicture = response.url;
    },
    //上传成功后的回调函数
    handleAvatarSuccess2(response) {
      this.practice.dbmhPicture = response.url;
    },
    //上传成功后的回调函数
    handleAvatarSuccess3(response) {
      this.practice.tzmhPicture = response.url;
    },
    //上传成功后的回调函数
    handleAvatarSuccess4(response) {
      this.practice.sgtxPicture = response.url;
    },
    handleAvatarSuccess5(response) {
      this.practice.lmPicture = response.url;
    },
    getPracticeByWordId() {
      this.stlxLoading = true;
      this.$http({
        url: this.$http.adornUrl(
          `/sf/cbpractice/getPracticeByWordId/${this.practice.tid}`
        ),
        method: "get",
      }).then(({ data }) => {
        this.$refs.practiceForm.resetFields();
        this.practice.id = null;
        this.practice.tid = this.$route.params.id;
        this.xzBtn = false;
        if (data && data.code === 0) {
          if (data.cbPractice) {
            this.type = 1;
            this.practice = data.cbPractice;
            this.xzBtn = true;
          } else {
            this.type = 0;
          }
        } else {
          // this.practice = { typefaceName: "", editionCover: "", id: null };
        }
        this.stlxLoading = false;
      });
    },
    // 章节对话框中的保存或修改
    saveOrUpdate() {
      if (this.type == 0) {
        // 添加章节
        this.practiceFormSubmit();
      } else {
        // 修改章节
        this.updatePractice();
      }
    },
    // 更新章节图片信息
    updatePractice() {
      this.$refs["practiceForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/sf/cbpractice/update`),
            method: "post",
            data: this.$http.adornData(
              {
                id: this.practice.id,
                tid: this.practice.tid,
                gcsxPicture: this.practice.gcsxPicture,
                dbmhPicture: this.practice.dbmhPicture,
                tzmhPicture: this.practice.tzmhPicture,
                sgtxPicture: this.practice.sgtxPicture,
                lmPicture: this.practice.lmPicture,
              },
              false
            ),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "修改成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.getPracticeByWordId();
                  this.dialogObserveVisible = false;
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
    // 打开对话框，添加、修改
    addObserve(status, id) {
      // 重置表单 应该只是把表单有的清空而已
      this.$nextTick(() => {
        this.$refs.practiceForm.resetFields();
      });
      if (status == 0) {
        // 添加
        this.type = 0;
        // 清空
        this.practice.id = null;
        this.practice.characterFeatureSrc = "";
      } else {
        // 修改
        this.type = 1;
        // 回显数据
        this.$http({
          url: this.$http.adornUrl("/sf/cbpractice/info/" + id),
          method: "get",
        }).then(({ data }) => {
          this.practice.id = data.cbPractice.id;
          this.practice.characterFeature = data.cbPractice.characterFeature;
          this.practice.tid = data.cbPractice.tid;
          this.practice.characterFeatureSrc =
            data.cbPractice.characterFeatureSrc;
        });
      }
      this.dialogObserveVisible = true;
    },
    // 表单提交
    practiceFormSubmit() {
      // 表单验证
      this.$refs["practiceForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/sf/cbpractice/save`),
            method: "post",
            data: this.$http.adornData(
              {
                id: this.practice.id,
                tid: this.practice.tid,
                gcsxPicture: this.practice.gcsxPicture,
                dbmhPicture: this.practice.dbmhPicture,
                tzmhPicture: this.practice.tzmhPicture,
                sgtxPicture: this.practice.sgtxPicture,
                lmPicture: this.practice.lmPicture
              },
              false
            ),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "添加成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.getPracticeByWordId();
                  // 只能添加一次
                  this.xzBtn = true;
                  this.dialogObserveVisible = false;
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
    // 上一步
    previous() {
      this.$router.push({
        path: "/ruanbi/word7",
      });
    },
    next() {
      this.saveOrUpdate();
    },
  },
};
</script>
<style scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.chanpterList {
  position: relative;
  list-style: none;
  margin: 0;
  padding: 0;
}
.chanpterList li {
  position: relative;
}
.chanpterList p {
  float: left;
  font-size: 20px;
  margin: 10px 0;
  padding: 10px;
  height: 70px;
  line-height: 50px;
  width: 100%;
  border: 1px solid #ddd;
}
.chanpterList .acts {
  float: right;
  font-size: 14px;
}

.videoList {
  padding-left: 50px;
}
.videoList p {
  float: left;
  font-size: 14px;
  margin: 10px 0;
  padding: 10px;
  height: 400px;
  line-height: 30px;
  width: 100%;
  border: 1px dotted #ddd;
}
</style>
